package com.my.study.hutool.core.io;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;

/**
 * @author: yidujun
 * @create: 2021/07/02 13:25
 */
@Slf4j(topic = "c.FileUtilTest")
public class FileUtilTest {

    private static final String PARENT_PATH = "D:/file";

    private static final String SRC_PATH = PARENT_PATH + "/test.txt";

    private static final String DEST_PATH = PARENT_PATH + "/test2.txt";

    public static void main(String[] args) {
        File touch = FileUtil.touch(PARENT_PATH + "/touch/test.txt");
        File touch1 = FileUtil.touch(PARENT_PATH + "/touch/t/test.txt");
        log.debug("fileName: {}", touch.getName());
    }

    /**
     * 从倒数第二个字符开始遍历查找{@code '/'}第一次出现的位置
     *
     */
    private static void testFind() {
        String fileName = "/s/x/t//";
        int length = fileName.length();
        log.debug("[length: {}]", length);
        int lastIndexOf = fileName.lastIndexOf("/", fileName.length() - 2);
        log.debug("lastIndexOf: [{}]", lastIndexOf);
    }

    private static void testFile() {
        File file = FileUtil.file(PARENT_PATH, "/aaaa.txt");
        String absolutePath = file.getAbsolutePath();
        log.debug("fileName: {}, absolutePath: {}", file.getName(), absolutePath);
    }

    private static void testFileCopy() {
        File copy = FileUtil.copy(SRC_PATH, DEST_PATH, false);
        String name = copy.getName();
        System.out.println(name);
    }

    /**
     * 根据字节输入流和输出流实现文件拷贝
     *
     * @see FileUtil
     * @see IoUtil
     */
    private static void testFileCopyByIOUtil() {
        BufferedInputStream in = FileUtil.getInputStream(SRC_PATH);
        BufferedOutputStream out = FileUtil.getOutputStream(DEST_PATH);
        long copy = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE);
        System.out.println(copy);
    }
}
